﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Turquoise.Helpers
{
    public static class StringHelper
    {

        public static int ConvertToInt(this string str)
        {
            var numAmount = 0;
            if (!string.IsNullOrEmpty(str))
            {
                int.TryParse(str, out numAmount);
            }
            return numAmount;
        }
        public static bool ConvertToBoolean(this string str)
        {
            string[] trueStrings = { "1", "y", "yes", "true", "evet", "on" };
            string[] falseStrings = { "0", "n", "no", "false", "hayır", "hayir", "off" };
            if (trueStrings.Contains(str, StringComparer.OrdinalIgnoreCase)) return true;
            if (falseStrings.Contains(str, StringComparer.OrdinalIgnoreCase)) return false;
            throw new InvalidCastException("Yalnızca şu ifadeler dönüştürülür: " + string.Join(", ", trueStrings) + " ve " + string.Join(", ", falseStrings));
        }
        public static T ConvertToEnum<T>(string value)
        {
            return (T)Enum.Parse(typeof(T), value, true);
        }
        public static List<string> SplitStringToStringList(string fullString, char delimeter)
        {
            var splitString = fullString.Split(delimeter);
            return splitString.ToList();

        }
        public static string MergeStringListToString(List<string> stringList, char delimeter)
        {
            return stringList.Aggregate(string.Empty, (current, t) => current + (t + delimeter));
        }
        public static List<string> SplitStringByStringList(List<string> stringList, char delimeter, int index)
        {
            return stringList.Select(t => t.Split(delimeter)[index]).ToList();
        }
        public static string ConvertToEncoded(string str)
        {
            str = str.Replace("Ç", "c");
            str = str.Replace("ç", "c");
            str = str.Replace("Ğ", "g");
            str = str.Replace("ğ", "g");
            str = str.Replace("I", "i");
            str = str.Replace("ı", "i");
            str = str.Replace("İ", "i");
            str = str.Replace("i", "i");
            str = str.Replace("Ö", "o");
            str = str.Replace("ö", "o");
            str = str.Replace("Ş", "s");
            str = str.Replace("ş", "s");
            str = str.Replace("Ü", "u");
            str = str.Replace("ü", "u");
            str = str.Trim().ToLower();
            str = Regex.Replace(str, @"\s+", "-");
            str = Regex.Replace(str, @"[^A-Za-z0-9_-]", "");
            return str;
        }

        public static string[,] GetClassifiedValues(string fullText, string key)
        {
            var a = fullText.Split('|');
            var t = (from h in a where h.Contains(key) select h).ToList()[0];
            var j = t.Split('-')[1];
            var g = j.Split(',');
            var values = new string[g.Length, 2];
            for (var i = 0; i < g.Length; i++)
            {
                var y = g[i].Split('x');
                values[i, 0] = y[0];
                values[i, 1] = y[1];
            }
            return values;
        }

        public static int[,] StringArrayToIntArray(string[,] arrayToBeConverted)
        {
            var result = new int[arrayToBeConverted.Length / 2, 2];
            for (var i = 0; i < arrayToBeConverted.Length / 2; i++)
            {
                result[i, 0] = Convert.ToInt32(arrayToBeConverted[i, 0]);
                result[i, 1] = Convert.ToInt32(arrayToBeConverted[i, 1]);
            }
            return result;
        }

        public static string UppercaseWords(string value)
        {
            var array = value.ToCharArray();
            if (array.Length >= 1)
            {
                if (char.IsLower(array[0]))
                {
                    array[0] = char.ToUpper(array[0]);
                }
            }
            for (var i = 1; i < array.Length; i++)
            {
                if (array[i - 1] != ' ')
                {
                    continue;
                }
                if (char.IsLower(array[i]))
                {
                    array[i] = char.ToUpper(array[i]);
                }
            }
            return new string(array);
        }

        public static string UppercaseFirst(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            return char.ToUpper(s[0]) + s.Substring(1);
        }
        
        public static string TemplateParser(string templateText, string regExString, string value)
        {
            var regExToken = new Regex(regExString, RegexOptions.IgnoreCase);
            return regExToken.Replace(templateText, value);
        }
    }
}
